package com.ibm.cps.processors;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ibm.cps.message.MessageFactory;
import com.ibm.cps.message.MessageFields;
import com.ibm.cps.message.ParsedDataMessage;
import com.ibm.cps.message.RawDataMessage;
import com.ibm.datamodel.timeseries.SchemaFormat;
import com.ibm.datamodel.timeseries.TimeSeriesSchema;
import com.ibm.factories.MessageParserFactory;
import com.ibm.interfaces.ITimeSeriesSchema;
import com.ibm.util.Constants;
import com.ibm.util.JSONUtility;
import com.ibm.util.exception.CPSException;

/**
 * rule with multiple attributes
 * @author Jingjing Wang
 *  2015-6-5
 */
public class ProcessorUtil {

	Logger logger = LoggerFactory.getLogger(ProcessorUtil.class);

	public static final String schemaid = "0000000008e151b18071eae8";
	public static final String tenantid = "123456789012345678901239";
	public static final String inputProcessorid = "0000000000ed66138a01299f";
	public static final String dftProcessorid = "0000000000e8151c09017eae";
	public static final String naiveRuleProcessorid = "0000000000ceab2e30f31549";
	public static final String ftRuleProcessorid = "0000000000ceab2e30f31441";
	public static final String tfRuleProcessorid = "0000000000ceab2e30f31442";
	public static final String fttfRuleProcessorid = "0000000000ceab2e30f31443";
	public static final String ruleEngineProcessorid = inputProcessorid
			+ "rule";
	public static final String publisherid = "0000000000e8151c09017eaf";
	public static final String schemaFieldName = "phase";

	@Deprecated
	public static ITimeSeriesSchema constructSchemabyInt(int length)
			throws CPSException {
		ArrayNode arrayNode = JSONUtility.newArrayNode();
		for (int i = 0; i < length; i++) {
			ObjectNode node = JSONUtility.newObjectNode();
			node.put(MessageFields.NAME, schemaFieldName + i);
			node.put(MessageFields.DATATYPE, "int");
			arrayNode.add(node);
		}
		return new TimeSeriesSchema(tenantid, schemaid, arrayNode, -1);
	}

	public static HashMap<String, ITimeSeriesSchema> constructSchemaMap(
			int length) throws CPSException {
		HashMap<String, ITimeSeriesSchema> schemaMap = new HashMap<String, ITimeSeriesSchema>();
		schemaMap.put(Constants.SCHEMA_INHERIT, constructSchemabyInt(length));
		return schemaMap;
	}

	public static RawDataMessage createRawData(int tskey, int timestamp,
			int... data) throws CPSException {
		ObjectNode node = JSONUtility.newObjectNode();
		node.put(MessageFields.TSKEY, tskey);
		node.put(MessageFields.TIMESTAMP, timestamp);
		for (int i = 0; i < data.length; i++) {
			if (data[i] == Integer.MIN_VALUE) {
				node.putNull(schemaFieldName + i);
			} else {
				node.put(schemaFieldName + i, data[i]);
			}
		}
		RawDataMessage rawDataMessage = new RawDataMessage(tenantid, schemaid,
				inputProcessorid, tskey, node.toString().getBytes());
		return rawDataMessage;
	}

	@Deprecated
	public static ParsedDataMessage createParsedDataMessage(int tskey,
			int timestamp, int... data) throws CPSException {
		return MessageFactory.produceParsedDataMessage(MessageParserFactory.createMessageParser(SchemaFormat.json, 
				constructSchemabyInt(data.length)),
				createRawData(tskey, timestamp, data));
	}

	public static void main(String[] args) {
		try {
			ParsedDataMessage msg = createParsedDataMessage(1, 2,
					Integer.MIN_VALUE);
			msg.toString();
		} catch (CPSException e) {
			e.printStackTrace();
		}
	}
}
